﻿using System;
using System.Collections.Generic;
using ServerApi.Controllers;
using ServerApi.Models;
using ServerApi.Models.Menu;
using TuyennvLibrary.ServerApi.Controllers;
using TuyennvLibrary.Utl;
namespace ServerApi.administrator
{
    public partial class Default : System.Web.UI.Page
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Config.Request = Request;
                string domain = Request.Url.ToString().Replace(Request.Url.AbsolutePath, "");
                Config.domain = (!string.IsNullOrEmpty(Request.Url.Query)) ? domain.Replace(Request.Url.Query, "") : domain;
                Config.Server = Server;
            }
        }

        protected void BtnAccept_Click(object sender, EventArgs e)
        {
            string username = TxtUsername.Text;
            string password = Utilities.md5(TxtPass.Text);
            Account acc = AdminController.getAccount(username, password);
            if (AdminController.checkLogin(username, password))
            {
                Session["username"] = username;
                Session["password"] = password;
                Session["userid"] = acc.Id;
                Session[AdminController.SESSION_USERCURRENT] = acc;
                List<Role> listrole=new RoleModel().getListRoleBy(acc.Id);
                Session["listrole"] = listrole;
                Session["listmenu"] = AdminController.getListMenuCatgories(acc.Id); 
                string url = Request["page"];
                if (!string.IsNullOrEmpty(url))
                {

                    Response.Redirect(url);
                }
                else
                {
                    if (listrole.Count > 0)
                    {
                        Response.Redirect(listrole[0].PageName);
                    }
                    else
                    {
                        Utilities.Alert(Page, this.GetType(), "Bạn không có quyền vào trang nào");
                    }
                }
            }
            else
            {
                
                if(acc.Status!=1&&acc.Id>0)
                    Utilities.Alert(Page, this.GetType(), "Tài khoản của bạn đã bị khóa");
                else
                    Utilities.Alert(Page, this.GetType(), "Sai username hoặc password");
            }
            
        }
    }
}